import { useState } from 'react';
import { Button, Space, Form, Row, Col, Input, Table, Select, DatePicker } from 'antd';
import { PAGINATION_CONFIG } from '@/constants';
import type { StubFault, QueryStubFaultListParams } from './services';
import { queryStubFaultList } from './services'
import { useRequest } from 'ahooks';
import type { ColumnsType } from 'antd/es/table/interface';
import dayjs from 'dayjs';
import StationSelect from '@/components/StationSelect';
import { EquipmentType } from '@/pages/station/stubList/services';
import { enum2options } from '@/utils/enum2options';

const AppUserPage: React.FC = () => {
  const [searchForm] = Form.useForm()
  const [searchParams, setSearchParams] = useState<QueryStubFaultListParams>({
    pageNum: 1, pageSize: 10, 
    beginDay: dayjs().subtract(31, 'days').format('YYYYMMDD'),
    endDay: dayjs().format('YYYYMMDD'),
  })
  const list = useRequest(() => queryStubFaultList(searchParams), {
    refreshDeps: [searchParams],
  })

  const onFinish = (formValues: any) => {
    setSearchParams({
      ...searchParams,
      stationId: formValues.station?.[0]?.id,
      errorCode: formValues.errorCode,
      beginDay: formValues.dayRange?.[0]?.format('YYYYMMDD'),
      endDay: formValues.dayRange?.[1]?.format('YYYYMMDD'),
      stubId: formValues.stubId,
      stubType: formValues.stubType,
      pageNum: 1,
    })
  }

  const columns: ColumnsType<StubFault> = [
    {
      title: '设备编号',
      dataIndex: 'stubId',
      width: 120,
      render: val => val || '--'
    },
    {
      title: '故障码',
      dataIndex: 'errorCode',
      width: 100,
    },
    {
      title: '故障说明',
      dataIndex: 'errorName',
      width: 120,
    },
    {
      title: '设备类型',
      dataIndex: 'stubType',
      width: 120,
      render: val => EquipmentType[val] || '--'
    },
    {
      title: '场站名称',
      dataIndex: 'stationName',
      width: 160,
    },
    {
      title: '上报次数',
      dataIndex: 'num',
      width: 120,
    },
    {
      title: '最新上报时间',
      dataIndex: 'updateTime',
      width: 140,
      render: (val, record) => val || record.createTime ? dayjs(val || record.createTime).format('YYYY-MM-DD HH:mm:ss') : '--',
    },
    {
      title: '地区',
      dataIndex: 'cityName',
      width: 160,
    },
  ]
  
  return (
    <div className='container'>
      <div className="card search-card">
        <div className="form-area">
          <Form
            name='searchErrorForm' form={searchForm} 
            onFinish={onFinish}
            initialValues={{dayRange: [dayjs().subtract(31, 'days'), dayjs()]}}
          >
            <Row gutter={[20, 20]}>
              <Col span={6}>
                <Form.Item label='场站名称' name='station'>
                  <StationSelect allowClear inline />
                </Form.Item>
              </Col>
              <Col span={6}>
                <Form.Item label='设备ID' name='stubId'>
                  <Input placeholder='请输入' />
                </Form.Item>
              </Col>
              <Col span={6}>
                <Form.Item label='故障码' name='errorCode'>
                  <Input placeholder='请输入' />
                </Form.Item>
              </Col>
              <Col span={6}>
                <Form.Item label='设备类型' name='stubType'>
                  <Select placeholder='请选择' options={enum2options(EquipmentType)} style={{width: '100%'}} allowClear />
                </Form.Item>
              </Col>
              <Col span={6}>
                <Form.Item label='上报日期' name='dayRange'>
                  <DatePicker.RangePicker style={{width: '100%'}} allowClear={false} disabledDate={current => current.isAfter(dayjs(), 'day')} />
                </Form.Item>
              </Col>
              <Col span={6}>
                <Space>
                  <Button type='primary' htmlType='submit' loading={list.loading}>筛选</Button>
                  <Button htmlType='reset'>重置</Button>
                </Space>
              </Col>
            </Row>
          </Form>
        </div>
      </div>
      <div className="card content-card">
        <Table<StubFault>
          rowKey={record => `${record.stubId}_${record.day}_${record.errorCode}`}
          loading={list.loading}
          dataSource={list.data?.data}
          columns={columns}
          scroll={{y: 700, x: 'max-content'}}
          pagination={{
            ...PAGINATION_CONFIG,
            current: searchParams.pageNum,
            pageSize: searchParams.pageSize,
            total: list.data?.totalCount || 0,
            onChange: (page, pageSize) => {
              setSearchParams({
                ...searchParams,
                pageNum: page === searchParams.pageNum ? 1 : page,
                pageSize,
              })
            }
          }}
        />
      </div>
    </div>
  );
};

export default AppUserPage;
